package com.example.leetcode.day;


import com.example.leetcode.ListNode;

/**
 * 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
 */
public class Day21 {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode node1 = list1;
        ListNode node2 = list2;
        ListNode r = new ListNode();
        ListNode t = r;
        while (node1 != null && node2 != null) {
            if (node1.val >= node2.val) {
                r.next = node2;
                node2 = node2.next;
                r = r.next;
            }else {
                r.next = node1;
                node1 = node1.next;
                r = r.next;
            }
        }
        while (node1 != null ) {
            r.next = node1;
            node1 = node1.next;
            r = r.next;
        }
        while (node2 != null ) {
            r.next = node2;
            node2 = node2.next;
            r = r.next;
        }
        return t.next;
    }

    public static void main(String[] args) {
        ListNode node = new ListNode(4);
        ListNode node1 = new ListNode(2,node);
        ListNode node2 = new ListNode(1, node1);

        ListNode node3 = new ListNode(4);
        ListNode node4 = new ListNode(3,node3);
        ListNode node5 = new ListNode(1,node4);

        Day21 d = new Day21();
        d.mergeTwoLists(node2,node5);
    }
}
